package common;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

public class CommonTools {
	public static synchronized void writeLog(String fileName, String subFolder,
			String content, boolean serverLog) {
		String logFolder;
		
		if (serverLog) {
			logFolder = getproperty("server.properties", "logFolder");
		} else {
			logFolder = getproperty("client.properties", "logFolder");
		}

		String logPath = logFolder + File.separator + subFolder
				+ File.separator + fileName + ".log";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		File f = new File(logPath);
		try {
			if (!f.exists()) {
				if (!f.getParentFile().exists()) {
					f.getParentFile().mkdirs();
				}
				f.createNewFile();
			}
			BufferedWriter output = new BufferedWriter(new FileWriter(f, true));
			output.write(sdf.format(date) + " - " + content);
			output.newLine();
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean isLegalIpAddress(String iPAddress,
			String serverGeolocation) {
		String ipHead = iPAddress.substring(0, iPAddress.indexOf("."));

		if ((serverGeolocation.equals("NA") && ipHead.equals("132"))
				|| (serverGeolocation.equals("EU") && ipHead.equals("93"))
				|| (serverGeolocation.equals("AS") && ipHead.equals("182"))) {

			return true;
		}
		return false;
	}

	public static String getproperty(String fileName, String property) {
		Properties prop = new Properties();
		// we should come back here and use an absolute path
		InputStream fis = CommonTools.class.getClassLoader().getResourceAsStream(
				fileName);
		try {
			prop.load(fis);

		} catch (IOException e) {

			e.printStackTrace();
		}
		return prop.getProperty(property);
	}
	
	public static String getIORFileName(String service, String fileName,String geolocation) {
		
		Properties prop = new Properties();
		
		InputStream fis = CommonTools.class.getClassLoader().getResourceAsStream(
				fileName);
		try {
			prop.load(fis);

		} catch (IOException e) {

			e.printStackTrace();
		}
		
		String filePath =  prop.getProperty(service+"IORFile"+"_"+geolocation);
		File f = new File(filePath);
		try {
			if (!f.exists()) {
				if (!f.getParentFile().exists()) {
					f.getParentFile().mkdirs();
				}
				f.createNewFile();
			}			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return filePath;

	}
	public static String getIOR(String service, String geolocation,String fileName){
		
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader(
					CommonTools.getIORFileName(service,fileName+".properties",geolocation)));
			String ior = br.readLine();
			br.close();
			return ior;
		
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
	}
}
